<?php

/**
 * Class wich manage permission
 */
class BabiliPermission {
	
	//the db connector
	private $db;

	/**
	 * The construct load the db connector
	 * @param $db
	 */
	public function __construct($db){
		$this->db = $db;
	}
	
	
	/**
	 * Function to modify a label of a role
	 * @param $roleid
	 * @param $label
	 * @return bool true if successful, false else
	 */
	public function modify_role($roleid, $label){
		
		//if there is a missing argument return false
			
		if(!is_numeric($roleid) || empty($label)){
			return false;
		}else{
			
			$query = "UPDATE role SET ";
			$query_args = array();
		
			$query .= "label = :label " ;
			$query_args[':label'] = $label;
			
			$query .= "WHERE id = :roleid";
			$query_args[':roleid'] = $roleid;
			
			return $this->db->update($query, $query_args);
		}		
	}
	
	/**
	 * Function to assign a role to a user
	 * @param $userid
	 * @param $roleid, you can enter the int or the name of the role :
	 * 				  - 0 => 'ANONYMOUS',
	 *				  - 1 => 'CONTRIBUTOR',
	 * 				  - 2 => 'VALIDATOR',
	 *				  - 3 => 'ADMIN'
	 * @return bool true if successful, false else
	 */
	public function assign_permission($userid, $roleid){
		//if there is a missing argument return false
		if(!is_numeric($userid) || !is_numeric($roleid)){
			return false;
		}
		
		//insert in database
		$query = "INSERT INTO privileges (user_id, role_id)
				  VALUES (:user_id, :role_id)";
		$query_args = array(':user_id' => $userid,
							':role_id' => $roleid);
		
		if($this->db->update($query, $query_args)){
			//return true
			return true;			 
		}
		
	}
	
	/**
	 * Function to remove a permission to an user
	 * @param $userid
	 * @param $roleid, you can enter the int or the name of the role :
	 * 				  - 0 => 'ANONYMOUS',
	 *				  - 1 => 'CONTRIBUTOR',
	 * 				  - 2 => 'VALIDATOR',
	 *				  - 3 => 'ADMIN'
	 * @return bool true if successful, false else
	 */
	public function remove_permission($userid, $roleid){
		//if there is a missing argument return false
		if(!is_numeric($userid) || !is_numeric($roleid)){
			return false;
		}
		
		//delete from database
		$query = "DELETE FROM privileges WHERE user_id = :user_id AND role_id = :role_id";
				
		$query_args = array(':user_id' => $userid,
							':role_id' => $roleid);
		
		
		return $this->db->update($query, $query_args);
		
	}
	
	/**
	 * Function to assign a language validator
	 * @param $userid
	 * @param $language_id can be the id (so integer) or the ISO code (string with 2 caracters)
	 * @return bool true if successful, false else
	 */
	public function assign_validator ($userid, $language_id){
		//if there is a missing argument return false
		if(!is_numeric($userid) || !is_numeric($language_id)){
			return false;
		}
		
		//insert in database
		$query = "INSERT INTO language_validator (language_id, user_id)
				  VALUES (:language_id, :user_id)";
		
		$query_args = array(':language_id' => $language_id,
							':user_id' => $userid);
		
		return $this->db->update($query, $query_args);
		
	}
	
	/**
	 * Function to remove a language validator
	 * @param $userid
	 * @param $language_id can be the id (so integer) or the ISO code (string with 2 caracters)
	 * @return bool true if successful, false else
	 */
	public function remove_validator ($userid, $language_id){
		//if there is a missing argument return false
		if(!is_numeric($userid) || !is_numeric($language_id)){
			return false;
		}
		
		//delete from database
		$query = "DELETE FROM language_validator WHERE language_id = :language_id AND user_id = :user_id";
				
		$query_args = array(':language_id' => $language_id,
							':user_id' => $userid);
		
		return $this->db->update($query, $query_args);
		
	}
	
	
}